<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>商城后台</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/lib/layui-v2.5.5/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">
    <link rel="stylesheet" th:href="@{/lib/layui_ext/dtree/dtree.css}" >
    <link rel="stylesheet" th:href="@{/lib/layui_ext/dtree/font/dtreefont.css}" >
    <link rel="stylesheet" th:href="@{/lib/zTree/css/zTreeStyle/zTreeStyle.css}" >
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>

<span th:replace="common_page::common"></span>

<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">属性类型</label>
        <div class="layui-input-block">
            <select id="attributeType"   name="attributeType" lay-search="" lay-filter="attributeType">
                <option value="1" selected>全局属性</option>
                <option value="2" >销售属性</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">搜索状态</label>
        <div class="layui-input-block">
            <select id="queryStatus"  name="queryStatus" lay-search="" lay-filter="queryStatus">
                <option value="0" selected>不可搜索</option>
                <option value="1" >可被搜索</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">属性名</label>
        <div class="layui-input-block">
            <input type="text" name="attributeName" lay-verify="required" maxlength="100" lay-reqtext="不能为空" placeholder="请输入属性名" value="" class="layui-input">
            <tip>填写属性名。</tip>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">所属分类</label>
        <div class="layui-input-block">
            <input type="hidden" name="categoryId" id="categoryId" th:value="${categoryId}">
            <input type="text" readonly  id="selectCategoryId" th:value="${categoryName}" lay-verify="required"  placeholder="请选择所属分类" value="" class="layui-input" >
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">上级节点</label>
        <div class="layui-input-block">
            <input type="hidden" name="parentId" id="parentId">
            <input type="text" readonly  id="selectParentId" lay-verify="required"  placeholder="请选择所属上级" value="" class="layui-input" >
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">排序</label>
        <div class="layui-input-block">
            <input type="number" name="attributeSort"    placeholder="请输入排序" value="" class="layui-input" lay-verify="number">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">显示状态</label>
        <div class="layui-input-block">
            <input type="radio" name="showStatus" value="1" title="显示" checked="">
            <input type="radio" name="showStatus" value="0" title="隐藏">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注信息</label>
        <div class="layui-input-block">
            <textarea name="remark" class="layui-textarea" maxlength="255" placeholder="请输入备注信息"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script th:src="@{/lib/layui-v2.5.5/layui.js}" charset="utf-8"></script>
<script th:src="@{/lib/jquery-3.4.1/jquery-3.4.1.min.js}" charset="utf-8"></script>
<script>
    layui.config({
        base: basePath+'/lib/layui_ext/dtree/' //配置 layui 第三方扩展组件存放的基础目录
    }).extend({
        dtree: 'dtree' //定义该组件模块名
    }).use(['form','tree','dtree','upload','laydate'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        var tree = layui.tree;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            $.ajax({
                url:"[[@{/product/attribute/attributeKey/save}]]",
                contentType: "application/json; charset=utf-8",
                type:'POST',
                data:JSON.stringify(data.field),
                success:function(data){
                    if(data.code<=0) {
                        layer.msg(data.msg);
                        return false;
                    }
                    var index = layer.alert(data.msg, {
                        title: '提示信息'
                    }, function () {
                        // 关闭弹出层
                        layer.close(index);
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.location.reload();
                        parent.layer.close(iframeIndex);

                    });
                }
            });
            return false;
        });


        var setting = {
            async: {
                enable: true,
                url:"[[@{/}]]product/attribute/attributeKey/query/category/tree/pid",
                autoParam:["id", "pid", "name"],
                dataFilter: filter
            },
            data:{
                simpleData:{
                    enable: true,
                    idKey:'id',
                    pIdKey:'parentId',
                    rootPId: -1
                }
            },
            view:{
                showIcon: true
            },
            callback: {
                onClick: functionTreeClick
            }
        };


        function filter(treeId, parentNode, data) {
            var dataArray = data.data;
            if(dataArray!=null&&dataArray.length>0)
            {
                for(var i=0;i<dataArray.length;i++)
                {
                    dataArray[i].url=null;
                }
            }
            return dataArray;
        }
        function functionTreeClick(event, treeId, treeNode) {
            // $("#inp").val("id="+treeNode.id + ", name=" + treeNode.name + ",pId=" + treeNode.pId);

            $("#selectCategoryId").val(treeNode.name);
            $("#categoryId").val(treeNode.id);
            layer.close(index);
        }

        $("#selectCategoryId").click(function(){

            index = layer.open({
                title: '选择分类',
                type: 1,
                shade: 0.2,
                maxmin:true,

                area: ['20%', '60%'],
                content: "<ul id=\"categoryTree\" style='height:90%' class=\"ztree\"></ul>",
                success:function(layero){
                    loadding = layer.load();

                    $.ajax({
                        url:"[[@{/}]]product/attribute/attributeKey/query/category/tree/pid",
                        type:"POST",
                        dataType: "json",
                        success: function(data){
                            var zTreeObj = $.fn.zTree.init($("#categoryTree"),setting, data.data);
                        },
                        error: function(){

                        },
                        complete:function()
                        {
                            layer.close(loadding);
                        }
                    });

                }
            });
            $(window).on("resize", function () {
                layer.full(index);
            });


        });




        $("#selectParentId").click(function(){
            var categoryId = $("#categoryId").val();
            var attributeType = $('#attributeType option:selected').val();
            if(categoryId==null)
            {
                layer.msg("请选择分类");
                return ;
            }
            if(categoryId=="-1")
            {
                layer.msg("不能选择根分类");
                return ;
            }
            index = layer.open({
                title: '选择上级节点',
                type: 1,
                shade: 0.2,
                maxmin:true,

                area: ['20%', '60%'],
                content: "<div id='attributeKeyTree'></div>",
                success:function(layero){

                    loadding = layer.load();
                    $.ajax({
                        url:"[[@{/}]]product/attribute/attributeKey/query/tree/category/id",
                        contentType: "application/json; charset=utf-8",
                        type:'GET',
                        data:{categoryId:categoryId,attributeType:attributeType},
                        success:function(data){
                            if(data.code==1)
                            {
                                //渲染
                                var inst1 = tree.render({
                                    elem: '#attributeKeyTree'  //绑定元素
                                    ,data: data.data,
                                    click: function(obj){
                                        $("#selectParentId").val(obj.data.title);
                                        $("#parentId").val(obj.data.id);
                                        layer.close(index);
                                    }
                                });
                            }
                        },
                        complete:function()
                        {
                            layer.close(loadding);
                        }
                    });

                }
            });
            $(window).on("resize", function () {
                layer.full(index);
            });

        });

    });
</script>
<script type="text/javascript" th:src="@{/lib/zTree/js/jquery.ztree.core.js}"></script>
<script type="text/javascript" th:src="@{/lib/zTree/js/jquery.ztree.excheck.js}"></script>
<script type="text/javascript" th:src="@{/lib/zTree/js/jquery.ztree.exedit.js}"></script>
</body>
</html>